/**
 * 自定义标题栏，有左右两个button和中间一个TextView组成
 */
package com.kedll.library.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kedll.library.R;


public class MyTitleBar extends RelativeLayout {

    public final static int BACKBTN_ID = 0;
    public final static int RIGHTBTN_ID = 1;
    protected View backBtnLayout;
    protected TextView titleTxt;
    protected View rightBtnLayout;
    protected ImageView backBtn;
    protected ImageView rightBtn;
    protected OnRightBtnClickListener mRightBtnClickListener;
    protected TextView backBtnText;
    protected TextView rightBtnText;
    protected OnLeftBtnClickListener mLeftBtnClickListener;
    protected Context mContext;

    private CharSequence sTitle;
    private boolean backBtnVisibility;
    /**
     * 标题栏点击效果
     */
    private int animId = R.anim.scale_alpha;
    private boolean rightBtnVisibility;
    private int backBtnBackground;
    private int rightBtnBackground;
    private int textColor;
    private int textSize = 12;
    private CharSequence backText;
    private CharSequence rightText;
    private int btnTextColor;
    private int btnTextSize;

    public MyTitleBar(Context context) {
        this(context, null);
    }

    public MyTitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyTitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        // TODO Auto-generated constructor stub
        this.mContext = context;
        initView(context);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyTitleBar);
        backBtnBackground = a.getResourceId(R.styleable.MyTitleBar_backBtnBackground, 0);

        backBtnVisibility = a.getBoolean(R.styleable.MyTitleBar_backBtnVisibility, true);

        backText = a.getText(R.styleable.MyTitleBar_backBtnText);

        rightBtnBackground = a.getResourceId(R.styleable.MyTitleBar_rightBtnBackground, 0);

        rightBtnVisibility = a.getBoolean(R.styleable.MyTitleBar_rightBtnVisibility, true);

        rightText = a.getText(R.styleable.MyTitleBar_rightBtnText);

        sTitle = a.getText(R.styleable.MyTitleBar_text);

        textColor = a.getColor(R.styleable.MyTitleBar_titleColor, Color.parseColor("#3f3f3f"));
        textSize = a.getInt(R.styleable.MyTitleBar_titleSize, 16);

        btnTextColor = a.getColor(R.styleable.MyTitleBar_btnTextColor, Color.parseColor("#666666"));
        btnTextSize = a.getInt(R.styleable.MyTitleBar_btnTextSize, 14);

        setTitle(sTitle);//设置标题内容
        setTitleColor(textColor);//设置标题字体颜色
        setTitleSize(textSize);//设置标题字体大小
        setBtnTextSize(btnTextSize);
        setBtnTextColor(btnTextColor);

        if (backBtnVisibility) {

            if (backBtnBackground != 0) {
                setBackBtnBackground(backBtnBackground);//设置左边button背景
            }
            if (backText != null) {
                setBackBtnText(backText);
                backBtn.setVisibility(GONE);
            }

        } else {
            hideBackBtn();//隐藏左边button
        }

        if (rightBtnVisibility) {
            if (rightBtnBackground != 0) {
                setRightBtnBackground(rightBtnBackground);    //设置右边button背景
            }
            if (rightText != null) {
                setRightBtnText(rightText);
            }
        } else {
            hideRightBtn();//隐藏右边button
        }
        a.recycle();
    }

    public boolean isBackBtnVisibility() {
        return backBtnVisibility;
    }

    public void setBackBtnVisibility(boolean backBtnVisibility) {
        this.backBtnVisibility = backBtnVisibility;
        if (backBtnVisibility) {
            showBackBtn();
        } else {
            hideBackBtn();
        }
    }

    public boolean isRightBtnVisibility() {
        return rightBtnVisibility;
    }

    public void setRightBtnVisibility(boolean rightBtnVisibility) {
        this.rightBtnVisibility = rightBtnVisibility;
        if (rightBtnVisibility) {
            showRightBtn();
        } else {
            hideRightBtn();
        }
    }

    protected void initView(final Context context) {
        /**
         * 构建backBtn布局
         *
         */
        LayoutInflater.from(context).inflate(R.layout.title_bar, this, true);
        backBtnLayout = findViewById(R.id.leftBtn_linear);
        backBtnLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mLeftBtnClickListener != null) {
                    mLeftBtnClickListener.onBack(v);
                } else {
                    ((Activity) context).overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                    ((Activity) context).onBackPressed();
                }
            }
        });


        /**
         * 构建rightBtn布局
         */
        rightBtnLayout = findViewById(R.id.rightbtn_linear);
        rightBtnLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mRightBtnClickListener != null) {
                    mRightBtnClickListener.onRightBtnClicked(v);
                }
            }
        });

        backBtn = findViewById(R.id.leftBtn);
        backBtnText = findViewById(R.id.leftBtn_text);

        rightBtn = findViewById(R.id.rightbtn);
        rightBtnText = findViewById(R.id.rightbtn_text);

        /**
         * 构建标题布局
         */
        titleTxt = findViewById(R.id.titlebar);
    }

    @Override
    protected void onFinishInflate() {
        // TODO Auto-generated method stub
        super.onFinishInflate();
    }

    /**
     * 设置标题
     *
     * @param txt
     */
    public void setTitle(CharSequence txt) {
        if (titleTxt != null) {
            titleTxt.setText(txt);
        }
    }

    /**
     * 返回标题内容
     *
     * @return
     */
    public CharSequence getTitle() {
        if (titleTxt == null) {
            return null;
        } else {
            return titleTxt.getText();
        }
    }

    public void setTitle(int titleRes) {
        if (titleTxt != null) {
            titleTxt.setText(titleRes);
        }
    }

    public void setBtnTextSize(int size) {
        backBtnText.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
        rightBtnText.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
    }

    public void setBtnTextColor(int color) {
        backBtnText.setTextColor(color);
        rightBtnText.setTextColor(color);
    }

    /**
     * 设置标题字体大小
     *
     * @param size
     */
    public void setTitleSize(int size) {
        if (titleTxt != null) {
            titleTxt.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
        }
    }

    /**
     * 设置标题字体颜色
     *
     * @param color
     */
    public void setTitleColor(int color) {
        if (titleTxt != null) {
            titleTxt.setTextColor(color);
        }
    }

    public void setTitleColor(ColorStateList color) {
        if (titleTxt != null) {
            titleTxt.setTextColor(color);
        }
    }

    /**
     * 获取返回键显示状态
     *
     * @return
     */
    public boolean isBackBtnShow() {
        return getBackBtn() != null && backBtnLayout.isShown();
    }

    public View getBackBtn() {
        return backBtnLayout;
    }

    public void setBackBtn(ImageButton backBtn) {
        setBackBtnVisibility(true);
        this.backBtn = backBtn;
    }

    /**
     * 显示返回键
     */
    public void showBackBtn() {
        backBtnLayout.setVisibility(View.VISIBLE);
        if (backText != null) {
            backBtnText.setVisibility(VISIBLE);
        }
        if (backBtnBackground != 0) {
            backBtn.setVisibility(VISIBLE);
        }
    }

    /**
     * 隐藏返回键
     */
    public void hideBackBtn() {
        backBtnLayout.setVisibility(View.INVISIBLE);
    }

    /**
     * 获取右边按钮显示状态
     *
     * @return
     */
    public boolean isRightBtnShow() {
        return getRightBtn() != null && rightBtnLayout.isShown();
    }

    public View getRightBtn() {
        return rightBtnLayout;
    }

    public void setRightBtn(ImageButton rightBtn) {
        setRightBtnVisibility(true);
        this.rightBtn = rightBtn;
    }

    /**
     * 显示右边按钮
     */
    public void showRightBtn() {
        rightBtnLayout.setVisibility(View.VISIBLE);

        if (rightBtnBackground != 0) {
            rightBtn.setVisibility(VISIBLE);
        }

        if (rightText != null) {
            rightBtnText.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏右边按钮
     */
    public void hideRightBtn() {
        rightBtnLayout.setVisibility(View.INVISIBLE);
    }

    /**
     * 设置返回按键背景
     *
     * @param id
     */
    public void setBackBtnBackground(int id) {
        backBtnBackground = id;
        setBackBtnVisibility(true);
        if (id != 0) {
            backBtn.setImageResource(id);
        } else {
            backBtn.setImageResource(R.drawable.btn_default);
        }
    }

    /**
     * 设置右边按键背景
     *
     * @param id
     */
    public void setRightBtnBackground(int id) {
        rightBtnBackground = id;
        setRightBtnVisibility(true);
        if (id != 0) {
            rightBtn.setImageResource(id);
        } else {
            rightBtn.setImageResource(R.drawable.btn_default);
        }
    }

    public TextView getTitleTxt() {
        return titleTxt;
    }

    public void setTitleTxt(TextView titleTxt) {
        this.titleTxt = titleTxt;
    }

    public int getAnimId() {
        return animId;
    }

    public void setAnimId(int animId) {
        this.animId = animId;
    }

    public OnRightBtnClickListener getRightBtnClickListener() {
        return mRightBtnClickListener;
    }

    public void setRightBtnClickListener(OnRightBtnClickListener mRightBtnClickListener) {
        this.mRightBtnClickListener = mRightBtnClickListener;
    }

    public OnLeftBtnClickListener getLeftBtnClickListener() {
        return mLeftBtnClickListener;
    }

    public void setLeftBtnClickListener(OnLeftBtnClickListener mLeftBtnClickListener) {
        this.mLeftBtnClickListener = mLeftBtnClickListener;
    }

    public CharSequence getBackBtnText() {
        return backText;
    }

    public void setBackBtnText(CharSequence backText) {
        setBackBtnVisibility(true);
        if (backText != null) {
            backBtnText.setText(backText);
        }
    }

    public void setRightBtnText(CharSequence rightText) {
        this.rightText = rightText;
        setRightBtnVisibility(true);
        if (rightText != null) {
            rightBtnText.setText(rightText);
        }
    }

    public CharSequence getRightBtnText() {
        return rightText;
    }

    public void setRightBtnText(int text) {
        rightBtnText.setText(text);
    }

    public void setLeftBtnText(int text) {
        backBtnText.setText(text);
    }

    /**
     * 左右button clicked事件接口
     */
    public interface OnRightBtnClickListener {
        void onRightBtnClicked(View view);
    }

    public interface OnLeftBtnClickListener {
        void onBack(View view);
    }
}
